// 测试Service代码转换
const fs = require('fs');
const path = require('path');

// 读取原始Service文件
const originalCode = fs.readFileSync('srcService/services/utilsService.ts', 'utf8');

console.log('=== 原始代码 ===');
console.log(originalCode);

// 模拟转换函数 - 通用版本
function transformServiceCode(code) {
  let transformed = code;
  
  // 1. 替换导入语句
  transformed = transformed.replace(
    /import ServiceBase from "\.\/ServiceBase";/g,
    'import Api from "@/api/Api";'
  );
  
  // 2. 替换类继承（保留类名）
  transformed = transformed.replace(
    /class (\w+) extends ServiceBase/g,
    'class $1 extends Api'
  );
  
  // 3. 替换构造函数（简化处理）
  transformed = transformed.replace(
    /constructor\(req: express\.Request = \{\} as express\.Request, res: express\.Response = \{\} as express\.Response\) \{\s*super\(req, res\);\s*\}/g,
    'constructor() {\n    super();\n  }'
  );
  
  // 4. 通用方法转换 - 匹配类内部的所有箭头函数方法
  // 匹配完整的箭头函数方法，包括多行内容
  transformed = transformed.replace(
    /(\w+)\s*=\s*\([^)]*\)\s*=>\s*\{[\s\S]*?\n\s*\};\s*$/gm,
    (match, methodName) => {
      return `${methodName}(data: any) {\n    return this._post("${methodName}", data);\n  }`;
    }
  );

  // 5. 清理可能残留的代码片段
  transformed = transformed.replace(/\s*}\s*catch\s*\([^)]*\)\s*\{[\s\S]*?\}\s*;\s*$/gm, '');
  transformed = transformed.replace(/\s*}\s*;\s*$/gm, '');
  
  return transformed;
}

// 执行转换
const transformedCode = transformServiceCode(originalCode);

console.log('\n=== 转换后代码 ===');
console.log(transformedCode);

// 验证转换结果
console.log('\n=== 转换验证 ===');
console.log('✅ 导入替换:', transformedCode.includes('import Api from "@/api/Api";'));
console.log('✅ 类继承替换:', transformedCode.includes('class UtilsService extends Api'));
console.log('✅ 构造函数简化:', transformedCode.includes('constructor() {'));
console.log('✅ 方法转换:', transformedCode.includes('get_ping(data: any) {'));
console.log('✅ this._post调用:', transformedCode.includes('this._post("get_ping", data)'));